package com.lier.gulimall.search.controller;

import com.lier.common.to.es.SkuEsModel;
import com.lier.common.utils.R;
import com.lier.common.valid.ExceptionTypeEnum;
import com.lier.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author liyd
 * @description 保存检索controller
 * @date 2025/6/30 17:00
 */
@Slf4j
@RequestMapping("/search/save")
@RestController
public class ElasticSaveController {

    @Resource
    private ProductSaveService productSaveService;

    /**
     * 上架商品保存商品用于检索的信息
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean b = false;
        try {
            b = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("ElasticSaveController商品上架错误: {}", e.getMessage());
            return R.error(ExceptionTypeEnum.PRODUCT_UP_EXCEPTION.getCode(),
                    ExceptionTypeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }

        if(b){
            return R.ok();
        }else {
            return R.error(ExceptionTypeEnum.PRODUCT_UP_EXCEPTION.getCode(),
                    ExceptionTypeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
    }
}
